package com.frank.oss.service;

import com.frank.oss.adapter.StorageAdapter;
import com.frank.oss.eneity.FileInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/2 15:05
 * @description
 */
@Service
public class FileService {

    private final StorageAdapter storageAdapter;

    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    public void createBucket(String bucketName) throws Exception {
        storageAdapter.createBucket(bucketName);
    }

    public String uploadFile(MultipartFile multipartFile, String bucketName, String objectName) throws Exception {
        storageAdapter.uploadFile(multipartFile,bucketName,objectName);
        objectName = objectName + "/" + multipartFile.getOriginalFilename();
        return storageAdapter.getUrl(bucketName, objectName);
    }

    public List<String> listBuckets() throws Exception {
        return storageAdapter.listBuckets();
    }

    public List<FileInfo> getAllObjects(String bucketName) throws Exception {
        return storageAdapter.getAllObjects(bucketName);
    }

    public InputStream download(String bucket, String objectName) throws Exception {
        return storageAdapter.download(bucket, objectName);
    }

    public void deleteBucket(String bucketName) throws Exception {
        storageAdapter.deleteBucket(bucketName);
    }

    public void deleteObject(String bucketName, String objectName) throws Exception {
        storageAdapter.deleteObject(bucketName, objectName);
    }

}
